/**
 * Created with JetBrains PhpStorm.
 * User: jiangshuguang
 * Date: 14-9-27
 * Time: 下午4:22
 * To change this template use File | Settings | File Templates.
 */
(function(){
    var util = require("wap_common:widget/ui/libs/util.js").util,
        $page = $(".page-register"),
        $tip = $page.find(".tip"),
        $username = $page.find(".phone"),
        $code = $page.find(".check-code"),
        $password = $page.find(".password"),
        $recommender = $page.find(".recommender"),
        $phoneBtn = $page.find(".btn-phone"),
        $registerBtn = $page.find(".btn-register");

    function tipShow(text){
        $tip.css("visibility","visible").text(text);
    }

    function tipHide(){
        $tip.css("visibility","hidden").text("");
    }

    //前段验证
    function check(){
        tipHide();
        if(!util.checkPhone($username.val())){
            tipShow("手机号码输入错误");
            return false;
        }else if(!(/^\d{6}$/.test($code.val()))){
            tipShow("验证码输入错误");
            return false;
        }else if(!util.checkPassword($password.val())){
            tipShow("密码不是6-16位字符");
            return false;
        }
        else if(!!($recommender.val()) && !(util.checkPhone($recommender.val()))){
            tipShow("推荐人手机填写错误");
            return false;
        }else if(!$("#protocol:checked").length){
            tipShow("请确认同意真融宝协议");
            return false;
        }else{
            return true;
        }
    }


    $phoneBtn.on("click",function(){
        if(!($(this).hasClass("btn-disable"))){
            var username = $.trim($username.val());
            if(util.checkPhone(username)){
                tipHide();
                $phoneBtn.addClass("btn-phone-disable").text("正在获取");
                $.post("/account/sendidentitycodewap",{
                    mobile:username,
                    type:$phoneBtn.attr("data-type")
                },function(ret,status){
                    if(ret.error_no == 0){
                        var count = 60,
                            countInterval = setInterval(function(){
                                $phoneBtn.text(count+"秒后重新获取");
                                if(count>0){
                                    count--;
                                }else{
                                    count = 60;
                                    clearInterval(countInterval);
                                    $phoneBtn.removeClass("btn-phone-disable")
                                        .text("未收到,语音获取")
                                        .attr("data-type",1);
                                }
                            },1000);
                    }else{
                        $phoneBtn.removeClass("btn-phone-disable");
                        tipShow(ret.error_message);
                    }
                },"json")
            }else{
                tipShow("手机号码输入错误");
            }
        }
    });

    $registerBtn.on("click",function(){
        if( !($(this).hasClass("btn-disable"))){
            if(check()){
                $(this).addClass("btn-disable");
                $.post("/account/registeruser",{
                    user_name:    $.trim($username.val()),
                    code:         $.trim($code.val()),
                    passwd:       $.trim($password.val()),
                    register_from:2,
                    recommender:  $.trim($recommender.val())
                },function(ret,status){
                    if( ret.error_no == 0){
                        location.href="/wap/wallet";
                    }else{
                        $registerBtn.removeClass("btn-phone-disable");
                        tipShow(ret.error_message);
                    }
                },"json")
            }
        }
    })
})();